# -*- coding: UTF-8 -*-
from flask import Flask, Response, render_template, json

from controller.controller_ocr import ocr
from utils.uitils_logger import Mylogger

'''static_folder 即可改变访问的路径和指定文件存在的位置'''
app = Flask(__name__, template_folder="./dist", static_folder="./dist/static")
'''注册路由'''
app.register_blueprint(ocr, url_prefix='/ocr')

# 日志路径
logger = Mylogger("app.log").get_logger()


@app.after_request
def cors(environ):
    environ.headers['Access-Control-Allow-Origin'] = '*'
    environ.headers['Access-Control-Allow-Method'] = '*'
    environ.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type'
    return environ

@app.errorhandler(500)
def error_date(error):
    logger.warning(f"当前错误码:【{error}】 返回欢迎语")
    result = {"message": 'success', "code": 0, "data": error}
    logger.warning(f"返回结果:【{result}】")
    return Response(json.dumps(result, ensure_ascii=False), content_type="application/json")

@app.route('/', methods=["GET"])
def hello_world():
    return render_template("index.html")


if __name__ == '__main__':
    from waitress import serve
    print("服务启动中，端口：9333 [2025年8月2日]")
    serve(app, host='0.0.0.0', port=9333)